#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QValidator>
#include<QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i=1;i<=4;i++)
        for(int j=1;j<=6;j++)
        {
            my[i][j]=new Mypushbutton(this);
            my[i][j]->setGeometry(j*80+50,i*80+50,80,80);

            my[i][j]->p.x=i;
            my[i][j]->p.y=j;
            my[i][j]->setText(QString::number(my[i][j]->p.x)
                             +QString::number(my[i][j]->p.y));

           QIcon* icon=new QIcon(":/"+QString::number(mydate.map[i][j]));
            my[i][j]->setIcon(*icon);
            my[i][j]->setIconSize(QSize(40,40));
            connect(my[i][j],SIGNAL(clicked()),this,SLOT(on_my_clicked()));
        }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_my_clicked()
{
    qDebug()<<"********on_my_clicked****************";
    Mypushbutton* btn=qobject_cast<Mypushbutton*>(sender());

    count++;
    if(count==1)
    {
        p1=btn;
       qDebug()<<p1<<"count="<<count;

    }
    if(count ==2)
    {
        p2=btn;
        if(p1==p2)
        {
            qDebug()<<p1<<p2<<"count="<<count;
            p1=nullptr; p2=nullptr; count=0;

        }
        else {
            if(mydate.canRemove(p1,p2))
            {
                qDebug()<<"*********can remove"<<p1<<p2<<"count="<<count;
                mydate.myremove(p1,p2);
               // p1=nullptr; p2=nullptr;
                //count=0;
            }



            p1=nullptr; p2=nullptr; count=0;
            qDebug()<<p1<<p2<<"count="<<count;

        }




    }
}
